{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import make_blobs\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.metrics import  calinski_harabasz_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "# 创建数据\n",
    "X, y = make_blobs(n_samples=1000, n_features=2, centers=[\n",
    "                  [-1, -1], [0, 0], [1, 1], [2, 2]], cluster_std=[0.4, 0.2, 0.2, 0.2], random_state=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "plt.scatter(X[:, 0], X[:, 1], marker=\"o\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "# kmeans训练,且可视化 聚类=2\n",
    "y_pre = KMeans(n_clusters=2, random_state=9).fit_predict(X)\n",
    "\n",
    "# 可视化展示\n",
    "plt.scatter(X[:, 0], X[:, 1], c=y_pre)\n",
    "plt.show()\n",
    "\n",
    "# 用ch_scole查看最后效果\n",
    "print( calinski_harabasz_score(X, y_pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "# kmeans训练,且可视化 聚类=3\n",
    "y_pre = KMeans(n_clusters=3, random_state=9).fit_predict(X)\n",
    "\n",
    "# 可视化展示\n",
    "plt.scatter(X[:, 0], X[:, 1], c=y_pre)\n",
    "plt.show()\n",
    "\n",
    "# 用ch_scole查看最后效果\n",
    "print( calinski_harabasz_score(X, y_pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "# kmeans训练,且可视化 聚类=4\n",
    "y_pre = KMeans(n_clusters=4, random_state=9).fit_predict(X)\n",
    "\n",
    "# 可视化展示\n",
    "plt.scatter(X[:, 0], X[:, 1], c=y_pre)\n",
    "plt.show()\n",
    "\n",
    "# 用ch_scole查看最后效果\n",
    "print( calinski_harabasz_score(X, y_pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
